package com.ruixun.shopmanagement.util.impl;

import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Base64;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Service;

import com.ruixun.shopmanagement.util.PropertiesUitl;
import com.ruixun.shopmanagement.util.inter.Mail_tool;

import sun.misc.BASE64Encoder;

@Service("QQMail")
public class QQMail implements Mail_tool {
	private String email;// 发件人邮箱
	private String code;// 激活码
	private String suggtionMail = "1246263205@qq.com";

	// 通过读取properties来发送
	public QQMail() {

		email = PropertiesUitl.getValue("mail.user");
		code = PropertiesUitl.getValue("mail.password");

	}

	/**
	 * 发送任意信息,创建一个实现了callable的mailrobot类,创建一个线程运行并通过futuretask去拿到结果,
	 * 
	 * @param String receiverMail 接受人的邮箱
	 * @param String words 发送内容
	 * @return boolean 发送结果
	 */

	@Override
	public boolean sendMail(String receiverMail, String words) {

		// TODO Auto-generated method stub
		String styleA1 = "<html><head></head><body><h1>";
		String styleA2 = "</h13></body></html>";
		MailRobot robot1 = new MailRobot(email, code, receiverMail, styleA1 + words + styleA2, "账号激活");
		FutureTask<Boolean> ftask = new FutureTask<Boolean>(robot1);
		new Thread(ftask).start();
		try {
			return ftask.get();
		} catch (InterruptedException | ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 将意见反馈发送到目标邮箱,创建一个实现了callable的mailrobot类,创建一个线程运行并通过futuretask去拿到结果,
	 * 
	 * @param String sender 发送人的邮箱
	 * @param String suggestion 发送内容
	 * @return Integer 验证码,0为发送失败
	 */
	@Override
	public boolean suggestionSendToMine(String sender, String suggestion) {
		// TODO Auto-generated method stub
		String styleA1 = "<html><head></head><body><h1>";
		String styleA2 = "</h13></body></html>";
		MailRobot robot1 = new MailRobot(email, code, suggtionMail,

				styleA1 + "建议" + suggestion + "发送人:" + sender + styleA2, "账号激活");
		FutureTask<Boolean> ftask = new FutureTask<Boolean>(robot1);
		new Thread(ftask).start();
		try {
			return ftask.get();
		} catch (InterruptedException | ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	// 判断是否为合法邮箱
	@Override
	public boolean isEmail(String string) {
		if (string == null)
			return false;
		String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		Pattern p;
		Matcher m;
		p = Pattern.compile(regEx1);
		m = p.matcher(string);
		if (m.matches())
			return true;
		else
			return false;
	}

	// 激活账号
	@Override
	public boolean sentCodeToActive(String email, String vcode) {
		String myIp = null;
		try {
			// 获取自己的ip地址
			myIp = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String myPort = "8082";
		String content = "<h1>来自天虎官方商城的激活邮件:请点击下面链接激活!</h1><h3><a href='http://" + myIp + ":" + myPort
				+ "/ShopManagement/user/active?method=active&code=" + vcode + "'>http://" + myIp + ":" + myPort
				+ "/userServlet?method=active&code=" + vcode + "</a></h3>";
		MailRobot robot1 = new MailRobot(email, code, email, content, "来自天虎官方商城的激活邮件!");
		FutureTask<Boolean> ftask = new FutureTask<Boolean>(robot1);
		new Thread(ftask).start();
		try {
			return ftask.get();
		} catch (InterruptedException | ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

}
